<?php
/**
 * Created by PhpStorm.
 * User: Jason
 * Date: 2017/8/8
 * Time: 23:32
 */

namespace Admin\Controller;

use Admin\Service\UserMemberService;
use Admin\Controller\AdminController;
use Admin\Service\UserService;
use Think\Page;
use Think\Log;


class UserMemberController extends AdminController
{
    /**
     * 获取会员列表
     * @param string $order
     * @param int $sort
     */
    public function index($order = 'u.id', $sort = 1)
    {
        if ($sort == 2) {
            $orderSort = $order.' desc';
        } else {
            $orderSort = $order;
        }
        $model = M('UserCoinWallet ucw');
        $join = 'user u ON ucw.user_id = u.id AND u.is_alias=0';
        $query = $model->field('u.id, count(*)')->join($join)->select();
        $count = $query[0]['count(*)'];
        $listRows = 15;
        $page = new Page($count, $listRows);
        if($count>$listRows){
            $page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        }
        $p =$page->show();
        $list = $model->field('u.id, ucw.amount, u.nickname, u.avatar_url')
            ->join($join)->order($orderSort)->select();

        $this->assign('_page', $p? $p: '');
        $this->assign('_total',$count);

        $this->assign('_list', $list);
        $this->assign('meta_title', '会员列表');
        $this->display('index');
    }
    /**
     * 会员收费设置
     */
    public function coinCharge()
    {
        $service = new UserMemberService();
        $list = $service->getChargeSet();
        $this->assign('_list', $list);
        $this->assign('meta_title', '收费配置');
        $this->display('coinCharge');
    }

    /**
     * 添加会员 收费设置
     */
    public function addChargeSet()
    {
        if (IS_POST) {
            $service = new UserMemberService();
            try {
                $id = $service->addChargeSet();
                $this->success('添加成功', 'index.php?s=Admin/UserMember/coinCharge');
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
        } else {
            $this->assign('meta_title', "新增会员设置");
            $this->display('add');
        }
    }

    /**
     * 删除 会员收费设置
     */
    public function delChargeSet()
    {
        $id = array_unique((array)I('id',0));

        $id = is_array($id) ? implode(',',$id) : $id;
        if (empty($id)) {
            $this->error('请选择要操作的数据!');
        }
        $map = array(
            'id' => array('in', $id)
        );

        $model = M('UserCoinCharge');
        $data = array(
            'updater_id' => UID,
            'update_time' => date('Y-m-d H:i:s', time()),
            'is_delete' => 1
        );
        $res = $model->where($map)->save($data);
        if ($res) {
            $this->success('删除成功');
        } else {
            echo $model->getLastSql();
            $this->error('删除失败');
        }
    }

    /**
     * 编辑 会员收费配置
     */
    public function editChargeSet($id = 0)
    {
        if (IS_POST) {
            $service = new UserMemberService();
            try {
                $res = $service->editChargeSet();
                $this->success('操作成功','index.php?s=Admin/UserMember/coinCharge');
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
        } else {
            //获取配置
            if ($id == 0) {
                $this->error('无效的参数');
            }
            $charge = M('UserCoinCharge')->find($id);
            if (!$charge) {
                $this->error('获取配置信息错误');
            }
            $this->assign('meta_title', '编辑会员收费配置');
            $this->assign('charge', $charge);
            $this->display('edit');
        }
    }

    /**
     * 马甲号列表
     */
    public function Alias()
    {
        $map = array(
            'is_alias' => 1,
            'is_delete' =>0
        );
        $list = M('User')->where($map)->field('id, avatar_url, nickname, create_time')->select();
        $this->assign('_list', $list);
        $this->assign('meta_title', '马甲列表');
        $this->display('alias');
    }

    /**
     * 添加马甲号
     */
    public function addAlias()
    {
        if (IS_POST) {
            $service = new UserService();
            try {
                $id = $service->addAlias();
                $this->success('添加成功', 'index.php?s=Admin/UserMember/Alias');
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
        } else {
            $this->assign('meta_title', "新增马甲号");
            $this->display('addAlias');
        }
    }

    /**
     * 编辑马甲号
     */
    public function editAlias()
    {
        if (IS_POST) {

        } else {
            $id = I('id');
            $aliasInfo = M('user')->where(array('id'=>$id))->field('id,nickname,avatar_url');
            $this->assign('info', $aliasInfo);
            $this->assign('meta_title', '编辑马甲');
            $this->display('editAlias');
        }


    }
    /**
     * 删除马甲号
     */
    public function delAlias()
    {
        $id = array_unique((array)I('id',0));

        $id = is_array($id) ? implode(',',$id) : $id;
        if (empty($id)) {
            $this->error('请选择要操作的数据');
        }
        $data = array('is_delete' => 1);
        $map = array('id' => array('in', $id) );
        $Model = M('User');
        if($Model->where($map)->save($data)){
            $this->success('删除成功');
        } else {
            echo $Model->getLastSql();
            $this->error('删除失败！');
        }
    }
}